package org.child_motivation.presentation.control;

import no.kantega.publishing.security.SecuritySession;
import no.kantega.publishing.security.data.User;
import org.child_motivation.business.model.Group;
import org.child_motivation.business.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class FrontPageController {

    @Autowired
    GroupService groupService;

    @RequestMapping(value="/frontpage")
    public String  viewFrontPage(ModelMap model, HttpServletRequest request){
        SecuritySession instance = SecuritySession.getInstance(request);
        User user = instance.getUser();
        if(true){
            List<Group> ownedGroupList = groupService.getGroups(user.getId());
            if(ownedGroupList.isEmpty()){
                return "redirect:creategroup";
            }
            model.put("ownedGroupList", ownedGroupList);
            return "frontpage-adult";
        }else{
            return "frontpage-child";
        }
    }
}